package com.gitee.wsl.android.ui.theme.inject


import android.annotation.SuppressLint
import android.content.res.ColorStateList
import android.os.Build
import android.widget.ImageView
import android.widget.TextView
import com.gitee.wsl.common.ui.theme.ColorPaletteScheme
import com.gitee.wsl.common.ui.theme.Theme
import com.gitee.wsl.common.ui.theme.TypographyScheme
import com.google.android.material.R
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.shape.MaterialShapeDrawable

@SuppressLint("PrivateResource")
fun <P: ColorPaletteScheme,T: TypographyScheme,THEME: Theme<P, T>> MaterialAlertDialogBuilder.theme(theme:THEME) {
    useTheme(this, theme.colorScheme.toAndroid()) { scheme ->
        val materialShapeDrawable = MaterialShapeDrawable(
            context,
            null,
            R.attr.alertDialogStyle,
            R.style.MaterialAlertDialog_MaterialComponents
        )
        materialShapeDrawable.initializeElevationOverlay(context)
        materialShapeDrawable.fillColor = ColorStateList.valueOf(scheme.surface)

        // dialogCornerRadius first appeared in Android Pie
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            val radius = theme.dimen.dialogBorderRadius
            materialShapeDrawable.setCornerSize(radius)
        }

        background = materialShapeDrawable
    }
}

fun <P: ColorPaletteScheme,T: TypographyScheme,THEME: Theme<P,T>> TextView.themeDialogHeadline(theme:THEME) {
    useTheme(this, theme.colorScheme.toAndroid()) { scheme ->
        setTextColor(scheme.onSurface)
    }
}

fun <P: ColorPaletteScheme,T: TypographyScheme,THEME: Theme<P,T>> TextView.themeDialogSupporting(theme:THEME) {
    useTheme(this, theme.colorScheme.toAndroid()) { scheme ->
        setTextColor(scheme.onSurfaceVariant)
    }
}

fun <P: ColorPaletteScheme,T: TypographyScheme,THEME: Theme<P,T>> ImageView.themeDialogIcon(theme:THEME) {
    useTheme(this, theme.colorScheme.toAndroid()) { scheme ->
        setColorFilter(scheme.secondary)
    }
}

